package ls.impl;

import ls.dao.CourseTeacherDAO;
import ls.entity.CourseTeacher;
import Connection.*;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class CourseTeacherDAOImpl  extends DAOBase implements CourseTeacherDAO {

    private static final String TEACHER_SELECT_SQL = "SELECT * FROM courseTeacher WHERE teacher_id=?";
    @Override
    public CourseTeacher getTeacher(String teacher_id) {
        CourseTeacher teacher = null;
        Connection connection = null;
        try {
            connection = getConnection();
            PreparedStatement preparedStatement = connection.prepareStatement(TEACHER_SELECT_SQL);
            preparedStatement.setString(1,teacher_id);
            ResultSet resultSet = preparedStatement.executeQuery();
            if(resultSet.next())
            {
                teacher = new CourseTeacher();
                teacher.setTeacher_id(resultSet.getString("teacher_id"));
                teacher.setTeacher_pwd(resultSet.getString("teacher_pwd"));
                teacher.setTeacher_name(resultSet.getString("teacher_name"));
            }
            resultSet.close();
            preparedStatement.close();
        }catch (Exception e)
        {
            e.printStackTrace();
        }finally {
            try {
                connection.close();
            }catch (Exception e)
            {
                e.printStackTrace();
            }
        }
        return teacher;
    }

    private static final String TEACHER_INSERT_SQL =
            "INSERT INTO courseTeacher VALUES(?,?,?) ";
    @Override
    public void addCourseTeacher(CourseTeacher courseTeacher) {
        Connection connection = null;
        try {
            connection = getConnection();
            PreparedStatement ps = connection.prepareStatement(TEACHER_INSERT_SQL);
            ps.setString(1,courseTeacher.getTeacher_id());
            ps.setString(2,courseTeacher.getTeacher_pwd());
            ps.setString(3,courseTeacher.getTeacher_name());
            ps.executeUpdate();
            ps.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            try {
                connection.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
